package com.hch.datastructure.lru;

/**
 * @author HeCongHui
 * @date 2022/6/23 17:12
 */
public class DoubleList {
    /**
     * 头尾虚节点
     */
    private Node head, tail;
    /**
     * 链表元素数
     */
    private int size;

    public DoubleList() {
//        初始化双向链表数据
        this.head = new Node(0, 0);
        this.tail = new Node(0, 0);
        this.head.setNext(this.tail);
        this.tail.setPre(this.head);
        this.size = 0;
    }

    /**
     * 向尾部添加节点
     *
     * @param last
     */
    public void addLast(Node last) {
        last.setPre(this.tail.getPre());
        last.setNext(this.tail);
        this.tail.getPre().setNext(last);
        this.tail.setPre(last);
        this.size++;

    }

    /**
     * 删除链表中的del节点，该节点一定存在
     *
     * @param del
     */
    public void remove(Node del) {
        del.getNext().setPre(del.getPre());
        del.getPre().setNext(del.getNext());
        this.size--;
    }

    public Node removeFirst() {
        Node first = this.head.getNext();
        if (first == this.tail) {
            return null;
        }
        remove(first);
        return first;
    }

    public Node getHead() {
        return head;
    }

    public void setHead(Node head) {
        this.head = head;
    }

    public Node getTail() {
        return tail;
    }

    public void setTail(Node tail) {
        this.tail = tail;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }
}
